import { MenuEnum } from '@/enums/editPageEnum';

// * 默认单组件选项
export const defaultOptions = [
	{
		label: '锁定',
		key: MenuEnum.LOCK,
		icon: 'LockClosedOutline',
		fnHandle: 'setLock'
	},
	{
		label: '解锁',
		key: MenuEnum.UNLOCK,
		icon: 'LockOpenOutline',
		fnHandle: 'setUnLock'
	},
	{
		label: '隐藏',
		key: MenuEnum.HIDE,
		icon: 'EyeOffOutline',
		fnHandle: 'setHide'
	},
	{
		label: '显示',
		key: MenuEnum.SHOW,
		icon: 'EyeOutline',
		fnHandle: 'setShow'
	},
	{
		divider: true
	},
	{
		label: '复制',
		key: MenuEnum.COPY,
		icon: 'Copy',
		fnHandle: 'setCopy'
	},
	{
		label: '剪切',
		key: MenuEnum.CUT,
		icon: 'Cut',
		fnHandle: 'setCut'
	},
	{
		label: '粘贴',
		key: MenuEnum.PARSE,
		icon: 'ClipboardOutline',
		fnHandle: 'setParse'
	},
	{
		divider: true
	},
	{
		label: '置顶',
		key: MenuEnum.TOP,
		icon: 'UpToTop',
		fnHandle: 'setTop'
	},
	{
		label: '置底',
		key: MenuEnum.BOTTOM,
		icon: 'DownToBottom',
		fnHandle: 'setBottom'
	},
	{
		label: '上移',
		key: MenuEnum.UP,
		icon: 'ChevronUp',
		fnHandle: 'setUp'
	},
	{
		label: '下移',
		key: MenuEnum.DOWN,
		icon: 'ChevronDown',
		fnHandle: 'setDown'
	},
	{
		divider: true
	},
	{
		label: '清空剪贴板',
		key: MenuEnum.CLEAR,
		icon: 'PaintBrush',
		fnHandle: 'setRecordChart'
	},
	{
		label: '删除',
		key: MenuEnum.DELETE,
		icon: 'Trash',
		fnHandle: 'removeComponentList'
	}
];

// * 默认多选组件选项
export const defaultMultiSelectOptions = [
	{
		label: '创建分组',
		key: MenuEnum.GROUP,
		icon: 'Carbon3DSoftware',
		fnHandle: 'setGroup'
	},
	{
		label: '解除分组',
		key: MenuEnum.UN_GROUP,
		icon: 'Carbon3DCursor',
		fnHandle: 'setUnGroup'
	}
];

// * 无数据传递拥有的选项
const defaultNoItemKeys = [MenuEnum.PARSE, MenuEnum.CLEAR];

/**
 * * 挑选选项
 * @param options
 * @param pickList
 * @returns
 */
const pickOption = (options, pickList) => {
	if (!pickList) return options;
	const list = [];
	pickList.forEach((e) => {
		list.push(...options.filter((op) => op.key === e));
	});
	return list;
};

/**
 * * 去除选项
 * @param options
 * @param hideList
 * @returns
 */
const hideOption = (options, hideList) => {
	if (!hideList) return options;
	return options.filter((op) => {
		return hideList.findIndex((e) => e !== op.key) !== -1;
	});
};

// * 右键内容
export const menuOptions = ref([]);

// * 右键处理
export const handleContextMenu = (
	e,
	// 右键对象
	targetInstance,
	// 判断函数
	optionsHandle,
	// 隐藏选项列表
	hideOptionsList,
	// 挑选选项列表
	pickOptionsList
) => {
	e.stopPropagation();
	e.preventDefault();

	let target = e.target;
	while (target instanceof SVGElement) {
		target = target.parentNode;
	}

	$store.chart.setTargetSelectChart(targetInstance && targetInstance.id);

	// 隐藏旧列表
	$store.chart.rightMenuShow = false;

	// * 多选默认选项
	if ($store.chart.targetChart.selectId.length > 1) {
		menuOptions.value = defaultMultiSelectOptions;
	} else {
		// * 单选默认选项
		menuOptions.value = defaultOptions;
	}

	if (!targetInstance) {
		menuOptions.value = pickOption(toRaw(menuOptions.value), defaultNoItemKeys);
	}
	if (hideOptionsList) {
		menuOptions.value = hideOption(
			[...defaultMultiSelectOptions, { divider: true }, ...defaultOptions],
			hideOptionsList
		);
	}
	if (pickOptionsList) {
		menuOptions.value = pickOption(
			[...defaultMultiSelectOptions, { divider: true }, ...defaultOptions],
			pickOptionsList
		);
	}
	if (optionsHandle) {
		// 自定义函数能够拿到当前选项和所有选项
		menuOptions.value = optionsHandle(
			cloneDeep(toRaw(menuOptions.value)),
			[...defaultMultiSelectOptions, ...defaultOptions],
			targetInstance
		);
	}
	nextTick().then(() => {
		$store.chart.setMousePosition(e.clientX, e.clientY);
		$store.chart.rightMenuShow = true;
	});
};
